# Set compiling flags for MSVC

if(MSVC_VERSION LESS 1700)
	MESSAGE(FATAL_ERROR "Cannot support Microsoft Visual C++ version ${MSVC_VERSION}. Build will be stopped." )
endif()

set(CMAKE_CXX_FLAGS "/DWIN32 /D_WINDOWS /W4 /wd4503 /wd4251 /wd4275 /wd4819 /EHsc /GR /D_SECURE_SCL=0 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_SECURE_NO_WARNINGS /D_SCL_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_WARNINGS") 

#C4503: decorated name length exceeded, name was truncated
#C4251: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
#C4275: non – DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier'
#C4819: The file contains a character that cannot be represented in the current code page (number). Save the file in Unicode format to prevent data loss.

set(CMAKE_C_FLAGS ${CMAKE_CXX_FLAGS})

if(CMAKE_INFO_PLATFORM_BITNESS EQUAL 32)
	set(CMAKE_CXX_FLAGS_DEBUG "/Zi")
	set(CMAKE_CXX_FLAGS_RELEASE "/arch:SSE")
	set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/arch:SSE /Zi")
	set(CMAKE_CXX_FLAGS_MINSIZEREL "/arch:SSE")
else()
	set(CMAKE_CXX_FLAGS_DEBUG "/Zi")
	set(CMAKE_CXX_FLAGS_RELEASE "")
	set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/Zi")
	set(CMAKE_CXX_FLAGS_MINSIZEREL "")
endif()

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /Ob0 /RTC1 /MDd /GS /fp:precise /D_DEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2 /Ob2 /GS- /fp:fast /MD /DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /O2 /Ob2 /GS- /fp:fast /MD /DNDEBUG")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /O1 /Ob1 /GS- /fp:fast /MD /DNDEBUG")

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MP")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MP")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MP")

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")

set(CMAKE_C_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
set(CMAKE_C_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
set(CMAKE_C_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
set(CMAKE_C_FLAGS_MINSIZEREL ${CMAKE_CXX_FLAGS_MINSIZEREL})

foreach(FLAG_VAR
	CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
	set(${FLAG_VAR} "")
endforeach(FLAG_VAR)
foreach(FLAG_VAR
	CMAKE_EXE_LINKER_FLAGS_DEBUG CMAKE_MODULE_LINKER_FLAGS_DEBUG CMAKE_SHARED_LINKER_FLAGS_DEBUG)
	set(${FLAG_VAR} "/INCREMENTAL /DEBUG")
endforeach(FLAG_VAR)
foreach(FLAG_VAR
	CMAKE_EXE_LINKER_FLAGS_RELEASE CMAKE_MODULE_LINKER_FLAGS_RELEASE CMAKE_SHARED_LINKER_FLAGS_RELEASE)
	set(${FLAG_VAR} "/INCREMENTAL:NO /OPT:REF /OPT:ICF")
endforeach(FLAG_VAR)
foreach(FLAG_VAR
	CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO)
	set(${FLAG_VAR} "/INCREMENTAL:NO /DEBUG /OPT:REF /OPT:ICF")
endforeach(FLAG_VAR)
foreach(FLAG_VAR
	CMAKE_EXE_LINKER_FLAGS_MINSIZEREL CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL)
	set(${FLAG_VAR} "/INCREMENTAL:NO /OPT:REF /OPT:ICF")
endforeach(FLAG_VAR)

foreach(FLAG_VAR
	CMAKE_EXE_LINKER_FLAGS_RELEASE CMAKE_MODULE_LINKER_FLAGS_RELEASE CMAKE_SHARED_LINKER_FLAGS_RELEASE)
	set(${FLAG_VAR} "${${FLAG_VAR}} /LTCG")
endforeach(FLAG_VAR)
foreach(FLAG_VAR
	CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO)
	set(${FLAG_VAR} "${${FLAG_VAR}} /LTCG")
endforeach(FLAG_VAR)
foreach(FLAG_VAR
	CMAKE_EXE_LINKER_FLAGS_MINSIZEREL CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL)
	set(${FLAG_VAR} "${${FLAG_VAR}} /LTCG")
endforeach(FLAG_VAR)